<?php

/**
 * job actions.
 *
 * @package    jobeet
 * @subpackage job
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class jobActions extends sfActions
{
	public function executeIndex(sfWebRequest $request)
	{

		$q = Doctrine_Query::create()
		->from('Job j')
		->where('j.created_at > ?', date('Y-m-d h:i:s', time() - 86400 * 30));

		$this->job_list = $q->execute();

	}

	public function executeShow(sfWebRequest $request)
	{
		$this->job = $this->getRoute()->getObject();
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new JobForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod('post'));

		$this->form = new JobForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($job = Doctrine::getTable('Job')->find($request->getParameter('id')), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
		$this->form = new JobForm($job);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
		$this->forward404Unless($job = Doctrine::getTable('Job')->find($request->getParameter('id')), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
		$this->form = new JobForm($job);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$this->forward404Unless($job = Doctrine::getTable('Job')->find($request->getParameter('id')), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
		$job->delete();

		$this->redirect('job/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()));
		if ($form->isValid())
		{
			$job = $form->save();

			$this->redirect('job/edit?id='.$job['id']);
		}
	}
}
